package com.donleo.stmg.entity.controller;


import com.donleo.stmg.common.api.CommonResult;
import com.donleo.stmg.entity.model.Daily;
import com.donleo.stmg.entity.service.DailyService;
import com.donleo.stmg.entity.vo.DailyVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 日报表 前端控制器
 * </p>
 *
 * @author liangd
 * @since 2020-12-21
 */
@RestController
@RequestMapping("/daily")
public class DailyController {

    @Autowired
    private DailyService dailyService;

    /**
     * 添加 已提交
     *
     * @param daily
     * @return
     */
    @PreAuthorize("hasAuthority('method:daily:create')")
    @PostMapping("/add")
    public CommonResult add(@RequestBody Daily daily) {
        return dailyService.add(daily);
    }

    /**
     * 保存为草稿
     *
     * @param daily
     * @return
     */
    @PreAuthorize("hasAuthority('method:daily:create')")
    @PostMapping("/save")
    public CommonResult save(@RequestBody Daily daily) {
        return dailyService.saveDaily(daily);
    }

    /**
     * 已查看
     *
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('method:daily:read')")
    @GetMapping("/showed")
    public CommonResult showed(Integer id) {
        return dailyService.showed(id);
    }

    @PreAuthorize("hasAuthority('method:daily:delete')")
    @PostMapping("/deletes")
    public CommonResult deletes(Integer[] ids) {
        List<Integer> asList = Arrays.asList(ids);
        return dailyService.deletes(asList);
    }

    @PreAuthorize("hasAuthority('method:daily:update')")
    @PostMapping("/update")
    public CommonResult update(@RequestBody Daily daily) {
        Assert.notNull(daily.getId(), "Id is not to be null");
        return dailyService.updateDaily(daily);
    }

    @PreAuthorize("hasAuthority('method:daily:read')")
    @GetMapping("/findById")
    public CommonResult findById(Integer id) {
        Daily daily = dailyService.findById(id);
        return CommonResult.success(daily);
    }

    @PreAuthorize("hasAuthority('method:daily:read')")
    @GetMapping("/findAll")
    public CommonResult findAll(DailyVo dailyVo) {
        List<Daily> list = dailyService.findAll(dailyVo);
        long total = dailyService.getCount(dailyVo);
        return CommonResult.success(total, list);
    }

    /**
     * 根据word模板生成新的word
     *
     * @param daily
     * @return
     */
    @PreAuthorize("hasAuthority('method:daily:create')")
    @PostMapping("/exportWord")
    public void exportWord(@RequestBody Daily daily, HttpServletResponse response) {
        //导出
        String fileName = dailyService.exportWord(daily);
        //下载
        dailyService.downLoad(fileName,response);
    }
}
